package com.laoye.big_event.Controller;

import com.laoye.big_event.Pojo.Result;
import com.laoye.big_event.Utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;



@RestController
@RequestMapping("/upload")
public class UploadController {
//   请求路径：/upload   请求方式：POST  接口描述：该接口用于上传文件(单文件)  请求参数格式：multipart/form-data
    @PostMapping
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
//        把文件内容存储到本地磁盘
        String originalFilename = file.getOriginalFilename();
//        保证文件的名字是唯一的，从而防止文件覆盖
        String filename = UUID.randomUUID().toString()+ originalFilename.substring(originalFilename.lastIndexOf("."));
        String url=AliOssUtil.uploadFile(filename, file.getInputStream());
        return Result.success(url);
    }

}
